import { IFeidaoAiBrowserComponent } from '@dfeidao/atom-web/interfaces';
import set from '@dfeidao/atom-web/local/set';
import nodejs from '@dfeidao/atom-web/msg/nodejs';
import whoami from '@dfeidao/atom-web/msg/whoami';
import render from '@dfeidao/atom-web/render/render';
import s_put from '@dfeidao/atom-web/storage/put';
////////////////////////////////////////////////////////////////////////////////
import p001 from './p001';
import p002 from './p002';
import p003 from './p003';

////////////////////////////////////////////////////////////////////////////////
export default async function a000(fd: IFeidaoAiBrowserComponent) {
	const prid = fd.data.node.querySelector<HTMLSpanElement>('[data-feidao-id="user"]').getAttribute('prid');
	await s_put('productid', prid);

	set(fd, 'condition_node', { param: {} });
	set(fd, 'condition_evt', { param: {} });
	set(fd, 'criteria', {});
	set(fd, 'sort', { usercode: 1 });
	set(fd, 'name2title', {
		'usercode': '用户名',
		'user_name': '姓名',
		'phone': '手机号',
		'email': '邮件',
		'is_disabled': '是否禁用'
	});

	const user = await whoami();

	// console.log(user)
	if (user) {
		// await s_put('productid', user.productid);
		await s_put('user', user);
		// const prid = user.productid;
		const page_no = 1;
		set(fd, 'page_no', page_no);
		const page_size = 20;
		set(fd, 'page_size', page_size);
		const query = {
			filter: {},
			productid: prid,
			sort: { usercode: 1 },
			page_no,
			page_size
		};
		// 没有数据
		// console.log(query)
		const res = await nodejs<{ dataArray: object[], page_no: number, page_size: number, count: number }>('consumer/zj-000001/s001', query);

		if (res.dataArray.length === 0) {
			render(fd, '', p002, 'p002', 'inner');
		} else {
			if (res.count >= 20) {
				res.page_no = page_no;
				res.page_size = page_size;
				render(fd, res, p003, 'p003', 'inner');
			}
			render(fd, res.dataArray, p001, 'p001', 'inner');
		}

		// console.log(res)

	}

}
